package com.acme.bankapp.domain.bank;

import static com.acme.bankapp.service.bank.BankService.addClient;

import com.acme.bankapp.exceptions.bank.ClientExistsException;
import com.acme.bankapp.exceptions.bank.NotEnoughFundsException;
import com.acme.bankapp.exceptions.bank.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount{
	private int overdraft;
	
	public CheckingAccount(int initialValue, int overdraft) throws IllegalArgumentException{
		super(initialValue);
		if(overdraft < 0){
			throw new IllegalArgumentException("Negative overdraft");
		}
		this.overdraft = overdraft;
	}

	public void withdraw(int sum) throws OverDraftLimitExceededException{
		if(sum > this.getBalance()&& sum <= this.getBalance() + overdraft){
			overdraft = this.getBalance() + overdraft - sum;
			this.setBalance(0);
		}
		else if(sum <= this.getBalance()){
			setBalance(this.getBalance() - sum);
		}
		else{
			throw new OverDraftLimitExceededException(this);
		}

	}
	
	public int getOverdraft(){
		return overdraft;
	}
	
	public int maximumAmountToWithdraw(){
		return this.getBalance() + overdraft;
	}
	public boolean equals(Object object){
		if(this == object){
			return true;
		}
		if(object == null){
			return false;
		}
		if(this.getClass() != object.getClass()){
			return false;
		}
		CheckingAccount ob = (CheckingAccount)object;
		if (this.getBalance() == ob.getBalance() && overdraft == ob.getOverdraft()){
			return true;
		}
		else{
			return false;
		}
	}
	
	public int hashCode(){
		int code = 11;
		final int k = 7;
		code = k * code + this.getBalance();
		code = k * code + this.getOverdraft();
		return code;
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer("Balance of this CheckingAccount is ");
		sb.append(AbstractAccount.decimalValue(this.getBalance()));
		sb.append(" and the overdraft is ");
		sb.append(AbstractAccount.decimalValue(overdraft));
		sb.append("\n");
		return sb.toString();
	}
}
